﻿Imports DataObject
Imports BusinessObject
Imports Microsoft.ApplicationBlocks.Data

Public Class frmNhanVienDetail

    Private ptyNhanVien As New NhanVien
    Private oNhanVien As New BusNhanVien
    Private oFunction As New hDCommonFunction

    Private Sub frmNhanVienDetail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If (Me.eActionMode = EditMode.AddNew) Then
            Me.Text = "Thêm mới nhân viên"
        Else
            Me.Text = "Sửa nhân viên"
        End If
        txtMaNV.Focus()
        LoadAllCombo()
        DataBindingToControl(GroupBox1)
    End Sub

    Private Sub LoadAllCombo()
        Dim ds As New DataSet
        ds.Clear()
        If (Not ds.Tables("ViTriCongViec") Is Nothing) Then
            ds.Tables("ViTriCongViec").Rows.Clear()
        End If
        SqlHelper.FillDataset(sConn, CommandType.StoredProcedure, "Proc_Get_ViTriCongViec_By_HoatDong", ds, New String() {"ViTriCongViec"})
        With cboChucVu
            .DataSource = ds.Tables("ViTriCongViec")
            .DisplayMember = "TenViTriCV"
            .ValueMember = "SoViTriCV"
            .DisplayLayout.Load(PathLayout & "cboChucVu.lyt")
        End With
    End Sub

    Public Overrides Function ValidateData() As Boolean
        If txtMaNV.Text = "" Then
            MessageBox.Show("Mã nhân viên không được để trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtMaNV.Focus()
            Return False
        End If
        If txtTenNV.Text = "" Then
            MessageBox.Show("Tên nhân viên không được để trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtTenNV.Focus()
            Return False
        End If
        Return True
    End Function

    Private Sub cboChucVu_ItemNotInList(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinEditors.ValidationErrorEventArgs) Handles cboChucVu.ItemNotInList
        cboChucVu.Text = ""
    End Sub

    Protected Overrides Function SaveDictionary() As Boolean
        Dim bResult As Boolean = False
        Try
            If ((Not cboChucVu.Value Is Nothing) Or (cboChucVu.Text.Equals("") = False)) Then
                ptyNhanVien.SoViTriCV = cboChucVu.Value
            Else
                ptyNhanVien.SoViTriCV = Nothing
            End If

            ptyNhanVien.MaNV = txtMaNV.Text
            ptyNhanVien.TenNV = txtTenNV.Text
            ptyNhanVien.GioiTinh = cboGioiTinh.Value
            ptyNhanVien.NgaySinh = CDate(dtNgaySinh.Value)
            ptyNhanVien.SoCMTND = txtSoCMTND.Text
            ptyNhanVien.NgayCap = CDate(dtNgayCap.Value)
            ptyNhanVien.NoiCap = txtNoiCap.Text
            ptyNhanVien.TKNganHang = txtTKNganHang.Text
            ptyNhanVien.DiaChiNganHang = txtDiaChiNganHang.Text
            ptyNhanVien.DiaChiGiaDinh = txtDiaChiGiaDinh.Text
            ptyNhanVien.DTCoQuan = txtDTCoQuan.Text
            ptyNhanVien.DTNhaRieng = txtDTNhaRieng.Text
            ptyNhanVien.DTDiDong = txtDTDiDong.Text
            ptyNhanVien.Email = txtEmail.Text

            If (eActionMode = EditMode.AddNew) Then
                oNhanVien.InsertNhanVien(sConn, ptyNhanVien)
            ElseIf (eActionMode = EditMode.Edit) Then
                ptyNhanVien.SoNV = RowDetail.Item("SoNV")
                oNhanVien.UpdateNhanVien(sConn, ptyNhanVien)
            End If

            bResult = True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            bResult = False
        End Try
        Return bResult
    End Function

    Private Sub cboChucVu_EditorButtonClick(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinEditors.EditorButtonEventArgs) Handles cboChucVu.EditorButtonClick
        Try
            Dim oRow As DataRow
            Dim ds As New DataSet
            Dim f As New frmViTriCongViecDetail
            Dim ListBindingSource As New System.Windows.Forms.BindingSource
            SqlHelper.FillDataset(sConn, CommandType.StoredProcedure, "Get_All_ViTriCongViecs", ds, New String() {"ViTriCongViec"})
            oRow = ds.Tables("ViTriCongViec").NewRow
            With oRow
                .Item("MaViTriCV") = ""
                .Item("TenViTriCV") = ""
                .Item("DienGiai") = ""
                .Item("HoatDong") = True
            End With
            ds.Tables("ViTriCongViec").Rows.Add(oRow)
            ListBindingSource.DataSource = ds.Tables("ViTriCongViec")
            ListBindingSource.MoveLast()
            With f
                .ActionMode = EditMode.AddNew
                .BindingSource = ListBindingSource
                .RowDetail = oRow
                .ShowDialog()
            End With
            LoadAllCombo()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        
    End Sub

End Class
